
import { globalUtils } from '../common/js/global-utils/global-utils-all';
import $ from 'jquery';

/* SOURCE-CODE-START */

"use strict";


/**
 * 生成菜单栏 HTML 并返回
 * @param {Object[]} menuArray 
 * @returns {string}
 */
function createDashboardMenuBar(menuArray) {
  if (globalUtils.isNullOrUndefined(menuArray)) {
    throw new Error('argument#0 "menuArray" is null/undefined');
  }

  var htmlArray = [];
  appendDashboardMenuBar(htmlArray, menuArray, false);
  var htmlString = htmlArray.join('\n');

  return htmlString;
}

/**
 * 递归添加菜单栏 HTML
 * @param {string[]} htmlArray 
 * @param {Object[]} menuArray 
 * @param {boolean} childFlag
 */
function appendDashboardMenuBar(htmlArray, menuArray, childFlag) {
  if (globalUtils.isNullOrUndefined(htmlArray)) {
    throw new Error('argument#0 "htmlArray" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(menuArray)) {
    throw new Error('argument#1 "menuArray" is null/undefined');
  }

  var menuContainerClassNames = ['dashboard-menu-container'];
  if (childFlag) {
    menuContainerClassNames.push('dashboard-menu-children');
  }

  var menuContainerStartTag = globalUtils.formatString('<ul class="{0}">',
    menuContainerClassNames.join(' '));
  htmlArray.push(menuContainerStartTag);

  for (var index = 0; index < menuArray.length; index++) {
    var menuInfo = menuArray[index];
    var menuName = globalUtils.escapeHtml(menuInfo.name);
    var menuUrl = menuInfo.path;
    var menuIcon = globalUtils.escapeHtml(menuInfo.icon);
    var children = menuInfo.children;

    var menuItemClassNames = ['dashboard-menu-item'];
    var menuItemInnerHtml;

    if (globalUtils.isNotEmptyString(menuIcon)) {
      menuItemInnerHtml = globalUtils.formatString('<i data-feather="{0}"></i>&nbsp;{1}', menuIcon, menuName);
    } else {
      menuItemInnerHtml = menuName;
    }

    if ((!globalUtils.isNullOrUndefined(children) && children.length > 0) || (menuUrl === '#')) {
      menuItemClassNames.push('dashboard-menu-dropdown-item');
      menuItemInnerHtml = menuItemInnerHtml + '<i class="dashboard-menu-dropdown-toggle"></i>';
    }

    if (!childFlag && index === 0) {
      menuItemClassNames.push('dashboard-menu-item-default');
    }

    if (menuUrl.startsWith('/')) {
      menuUrl = menuUrl.substring(1);
    }

    var linkUrl = '#/' + menuUrl;
    if (menuUrl === '#') {
      linkUrl = globalUtils.getFullUrl(Pure.config.singlePageBaseUrl);
    } else {
      linkUrl = globalUtils.getFullUrl(Pure.config.singlePageBaseUrl) + linkUrl;
    }

    // 菜单项开始标签
    var menuItemStartTag = globalUtils.formatString('<li class="{0}">',
      menuItemClassNames.join(' '));
    htmlArray.push(menuItemStartTag);
    // 菜单项链接
    var menuItemLinkFormat = '<a class="dashboard-menu-item-link" href="{0}" data-menu-url="{1}">{2}</a>';
    var menuItemLinkHtml = globalUtils.formatString(menuItemLinkFormat,
      linkUrl, menuUrl, menuItemInnerHtml);
    htmlArray.push(menuItemLinkHtml);

    // 生成子菜单 HTML
    if (!globalUtils.isNullOrUndefined(children) && children.length > 0) {
      appendDashboardMenuBar(htmlArray, children, true);
    }

    // 菜单项结束标签
    htmlArray.push('</li>');
  }

  htmlArray.push('</ul>');
}

/**
 * 渲染菜单栏
 * @param {string} menuHtml 
 * @param {function} clickFn 
 */
function renderDashboardMenuBar(menuHtml, clickFn) {
  if (globalUtils.isNullOrUndefined(menuHtml)) {
    throw new Error('argument#0 "menuHtml" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(clickFn)) {
    throw new Error('argument#1 "clickFn" is null/undefined');
  }

  var jqMenuBar = $('.dashboard-menu-bar');
  // 渲染菜单栏
  jqMenuBar.html(menuHtml);
  // 显示菜单图标
  feather.replace({
    class: 'dashboard-menu-icon',
    width: 18,
    height: 18
  });

  // 点击菜单加载内嵌页面
  jqMenuBar.on('click', '.dashboard-menu-item-link', function (event) {
    event.preventDefault();
    var jqLink = $(this);
    var url = jqLink.data('menu-url');
    var jqMenu = jqLink.parent();

    if (jqMenu.hasClass('dashboard-menu-dropdown-item')) {
      var jqChildren = jqLink.siblings('.dashboard-menu-children');

      // 展开/收起子菜单
      jqMenu.toggleClass('dashboard-menu-item-expand');
      jqChildren.toggle(1, function () {
        if (!jqMenu.hasClass('dashboard-menu-item-expand')) {
          jqMenu.find('.dashboard-menu-item-expand')
            .removeClass('dashboard-menu-item-expand');
          jqMenu.find('.dashboard-menu-children')
            .css('display', 'none');
        }
      });
    } else {
      clickFn(url);
    }
  });
}

/* SOURCE-CODE-END */
